using System;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Web.Caching;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;

namespace TheBoard.Models
{
    public class OpenIdSessions : IOpenIdSessions
    {
        public OpenIdSessions()
        {
            SessionCookieName = "openidauth";
        }

        #region IOpenIdSessions Members

        public string SessionCookieName { get; set; }

        public KeyAndPrincipal CreatePrincipal(IAuthenticationResponse response)
        {
            var claimsResponse = response.GetExtension<ClaimsResponse>();
            var identity = new OpenIdIdentity
                               {
                                   ClaimedIdentifier = response.ClaimedIdentifier,
                                   Properties = new OpenIdIdentityProperties
                                                    {
                                                        //BirthDate = claimsResponse.BirthDate,
                                                        //Country = claimsResponse.Country,
                                                        //Culture = claimsResponse.Culture.Name,
                                                        Email = claimsResponse.Email,
                                                        FullName = claimsResponse.FullName,
                                                        //Gender = claimsResponse.Gender
                                                        //{"",claimsResponse.IsSignedByProvider},
                                                        //{"MailAdrdress",claimsResponse.MailAddress},
                                                        NickName = claimsResponse.Nickname
                                                        //PostalCode = claimsResponse.PostalCode,
                                                        //TimeZone = claimsResponse.TimeZone,
                                                    }
                               };

            var principal = new GenericPrincipal(identity, new string[0]);

            var key = GetMD5Hash(response.ClaimedIdentifier);
            return new KeyAndPrincipal {Key = key, Principal = principal};
        }

        private string GetMD5Hash(string s)
        {
            using (var hasher = new SHA1CryptoServiceProvider())
            {
                return Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(s)));
            }
        }

        public IPrincipal TryGetPrincipal(string key)
        {
            var cache = System.Web.HttpContext.Current.Cache;
            return cache.Get(key) as IPrincipal;
        }

        public void SetPrincipal(string key, IPrincipal principal)
        {
            var cache = System.Web.HttpContext.Current.Cache;
            cache.Add(key, principal, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.High,
                      null);
        }

        public void RemovePrincipal(string key)
        {
            var cache = System.Web.HttpContext.Current.Cache;
            cache.Remove(key);
        }

        #endregion
    }
}